package com.huawei.esdk.uc.u19.north.rest.app.resource.ctc;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.platform.common.utils.NumberUtils;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.uc.common.ErrInfo;
import com.huawei.esdk.uc.domain.model.bean.AttendeeInfo;
import com.huawei.esdk.uc.domain.model.u19.Attendee;
import com.huawei.esdk.uc.loggermgr.LoggerMgr;
import com.huawei.esdk.uc.u19.business.professional.rest.attendee.AttendeeService;
import com.huawei.esdk.uc.u19.north.rest.app.resource.attendee.convert.AttendeeResourceConvert;
import com.huawei.esdk.uc.u19.north.rest.bean.attendee.AttendeeRequest;
import com.huawei.esdk.uc.u19.north.rest.bean.attendee.AttendeeResponse;
import com.huawei.esdk.uc.u19.north.rest.bean.attendee.UpdateAttendeeRequest;
import com.huawei.esdk.uc.u19.north.rest.bean.attendee.UpdateAttendeeResponse;

@Path("uc/u19/attendee")
public class CTCAttendeeResource
{
    /**
     * 日志对象
     */
    private static final Logger LOGGER = Logger.getLogger(CTCAttendeeResource.class);
    
    /**
     * 与会人源转换对象
     */
    private static final AttendeeResourceConvert ATTENDEE_RESOURCE_CONVERT = new AttendeeResourceConvert();
    
    /**
     * 与会人服务层对象
     */
    private static final AttendeeService ATTENDEE_SERVICE = new AttendeeService();
    
    /**
     * 添加与会者
     * <功能详细描述>
     * @param attendeeRequest
     * @return
     * @see [类、类#方法、类#成员]
     */
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public AttendeeResponse addAttendee(AttendeeRequest attendeeRequest)
    {
        AttendeeResponse response = new AttendeeResponse();
        
        if (null == attendeeRequest || StringUtils.isEmpty(attendeeRequest.getUserId())
            || StringUtils.isEmpty(attendeeRequest.getGwIp()) || StringUtils.isEmpty(attendeeRequest.getConfRoom())
            || StringUtils.isEmpty(attendeeRequest.getAttendeeNumber())
            || StringUtils.isEmpty(attendeeRequest.getAttribute()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_COMPLETE_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_UC_PARAM_NOT_COMPLETE_ERRORCODESC);
            LoggerMgr.interfacelogger("POST", "addAttendee",
                attendeeRequest, response.getResultCode());
            return response;
        }
        
        // 校验gwIp参数的格式
        if (!StringUtils.isIp(attendeeRequest.getGwIp()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            LoggerMgr.interfacelogger("POST", "addAttendee",
                attendeeRequest, response.getResultCode());
            return response;
        }
        
        // 会议厅号和与会者权限必须为数字, 会议厅号不能超int范围
        if (!StringUtils.isInteger(attendeeRequest.getConfRoom())
            || !NumberUtils.isNumber(attendeeRequest.getAttribute()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            LoggerMgr.interfacelogger("POST", "addAttendee",
                attendeeRequest, response.getResultCode());
            return response;
        }
        
        // 与会者权限必须为0-4之间
        if (Integer.parseInt(attendeeRequest.getAttribute()) < 0
            || Integer.parseInt(attendeeRequest.getAttribute()) > 4)
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            LoggerMgr.interfacelogger("POST", "addAttendee",
                attendeeRequest, response.getResultCode());
            return response;
        }
        
        try
        {
            SDKResult<AttendeeInfo> result =
                ATTENDEE_SERVICE.addAttendee(ATTENDEE_RESOURCE_CONVERT.addAttendeeRest2Model(attendeeRequest));
            
            response.setResultCode(String.valueOf(null == result ? 1 : result.getErrCode()));
            response.setResultContext(StringUtils.avoidNull(null == result ? "" : result.getDescription()));
            response.setAttendeeInfo(null == result ? null : result.getResult());
        }
        catch (SDKException e)
        {
            LOGGER.error("addAttendee method SDK error", e);
            response.setResultCode(String.valueOf(e.getSdkErrCode()));
            response.setResultContext(e.getSdkErrDesc());
        }
        catch (Exception e)
        {
            LOGGER.error("addAttendee method error", e);
            response.setResultCode(String.valueOf(ErrInfo.SDK_SYSTEM_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_SYSTEM_ERRORDESC);
        }
        
        LoggerMgr.interfacelogger("POST", "addAttendee",
            attendeeRequest, response.getResultCode());
        
        return response;
    }
    
    /**
     * 修改与会者权限
     * 
     * @param attendee
     * @return
     */
    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public UpdateAttendeeResponse modifyAttendeeAuthority(UpdateAttendeeRequest attendee)
    {
        UpdateAttendeeResponse response = new UpdateAttendeeResponse();
        
        if (null == attendee || StringUtils.isEmpty(attendee.getUserId()) || StringUtils.isEmpty(attendee.getGwIp())
            || StringUtils.isEmpty(attendee.getConfRoom()) || StringUtils.isEmpty(attendee.getAttendeeNumber())
            || StringUtils.isEmpty(attendee.getAttribute()) || StringUtils.isEmpty(attendee.getIsRegister()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_COMPLETE_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_UC_PARAM_NOT_COMPLETE_ERRORCODESC);
            LoggerMgr.interfacelogger("PUT", "modifyAttendeeAuthority",
                attendee, response.getResultCode());
            return response;
        }
        
        // 校验gwIp参数的格式
        if (!StringUtils.isIp(attendee.getGwIp()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            LoggerMgr.interfacelogger("PUT", "modifyAttendeeAuthority",
                attendee, response.getResultCode());
            return response;
        }
        
        // 会议厅号和与会者权限必须为数字, 会议厅号不能超int范围
        if (!StringUtils.isInteger(attendee.getConfRoom()) || !NumberUtils.isNumber(attendee.getAttribute()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            LoggerMgr.interfacelogger("PUT", "modifyAttendeeAuthority",
                attendee, response.getResultCode());
            return response;
        }
        
        if (!"0".equals(attendee.getAttribute()) && !"1".equals(attendee.getAttribute())
            && !"2".equals(attendee.getAttribute()) && !"3".equals(attendee.getAttribute())
            && !"4".equals(attendee.getAttribute()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            LoggerMgr.interfacelogger("PUT", "modifyAttendeeAuthority",
                attendee, response.getResultCode());
            return response;
        }
        
        // 是否注册必须为0或1
        if (!"0".equals(attendee.getIsRegister()) && !"1".equals(attendee.getIsRegister()))
        {
            response.setResultCode(String.valueOf(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_UC_PARAM_NOT_CORRECT_ERRORCODESC);
            LoggerMgr.interfacelogger("PUT", "modifyAttendeeAuthority",
                attendee, response.getResultCode());
            return response;
        }
        
        Attendee attendeeConvert = ATTENDEE_RESOURCE_CONVERT.getModifyAttendeeAuthorityRest2Model(attendee);
        
        try
        {
            SDKErrorCode result = ATTENDEE_SERVICE.modifyAttendeeAuthority(attendeeConvert);
            
            response.setResultCode(String.valueOf(result.getErrCode()));
            response.setResultContext(StringUtils.avoidNull(result.getDescription()));
        }
        catch (SDKException e)
        {
            LOGGER.error("updateAttendee method SDK error", e);
            response.setResultCode(String.valueOf(e.getSdkErrCode()));
            response.setResultContext(e.getSdkErrDesc());
        }
        catch (Exception e)
        {
            LOGGER.error("updateAttendee method error", e);
            response.setResultCode(String.valueOf(ErrInfo.SDK_SYSTEM_ERRORCODE));
            response.setResultContext(ErrInfo.SDK_SYSTEM_ERRORDESC);
        }
        
        LoggerMgr.interfacelogger("PUT", "modifyAttendeeAuthority",
            attendee, response.getResultCode());
        
        return response;
    }
}
